package com.woniu.cq.medical.service.impl;

import com.woniu.cq.medical.controller.form.ActForm;
import com.woniu.cq.medical.dao.ActOrderInfoMapper;
import com.woniu.cq.medical.entity.ActOrder;
import com.woniu.cq.medical.dao.ActOrderMapper;
import com.woniu.cq.medical.entity.ActOrderInfo;
import com.woniu.cq.medical.service.IActOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2020-09-10
 */
@Service
public class ActOrderServiceImpl extends ServiceImpl<ActOrderMapper, ActOrder> implements IActOrderService {

    @Autowired
    private ActOrderMapper actOrderMapper;
    @Autowired
    private ActOrderInfoMapper actOrderInfoMapper;

    @Override
    public ActForm selectPatient(String patientCardId) {

        ActForm actForm = actOrderMapper.selectPatient(patientCardId);
        return actForm;
    }

    @Override
    public ActForm seleAct(String patientIdCase, String caseId) {
        int id=Integer.parseInt(caseId);
        ActForm actForm = actOrderMapper.seleAct(patientIdCase, id);
        return actForm;
    }

    @Override
    @Transactional
    public void submit(ActForm actForm) {
        String patientCardId=actForm.getPatientIdCard();

        int caseId=actForm.getCaseId();
        ActOrder actOrder = actOrderMapper.selectActOne(patientCardId, caseId);
        int doctorId = actOrderMapper.selectByName(actForm.getDoctorName());
        Integer actOrderId=-1;
        if(actOrder==null){
            ActOrder act=new ActOrder();
            act.setCaseId(actForm.getCaseId());
            act.setActOrderType(actForm.getActOrderType());
            act.setActOrderResult(actForm.getActOrderResult());
            act.setActOrderStartTime(actForm.getActOrderStartTime());
            act.setActOrderEndTime(actForm.getActOrderEndTime());
            act.setActOrderCreatTime(new Date());
            act.setDoctorId(doctorId);
            act.setPatientAge(actForm.getPatientAge());
            act.setPatientSex(actForm.getPatientSex());
            act.setPatientIdCard(actForm.getPatientIdCard());
            act.setPatientName(actForm.getPatientName());
            actOrderMapper.insert(act);
            actOrderId = act.getActOrderId();

        }else {
            ActOrder act=new ActOrder();
            act.setActOrderId(actForm.getActOrderId());
            act.setCaseId(actForm.getCaseId());
            act.setActOrderType(actForm.getActOrderType());
            act.setActOrderResult(actForm.getActOrderResult());
            act.setActOrderStartTime(actForm.getActOrderStartTime());
            act.setActOrderEndTime(actForm.getActOrderEndTime());
            act.setActOrderCreatTime(new Date());
            act.setDoctorId(doctorId);
            act.setPatientAge(actForm.getPatientAge());
            act.setPatientSex(actForm.getPatientSex());
            act.setPatientIdCard(actForm.getPatientIdCard());
            act.setPatientName(actForm.getPatientName());
            actOrderMapper.updateById(act);
        }
        ActOrderInfo actOrderInfo=new ActOrderInfo();
        actOrderInfo.setCaseId(actForm.getCaseId());
        actOrderInfo.setActOrderType(actForm.getActOrderType());
        actOrderInfo.setActOrderResult(actForm.getActOrderResult());
        actOrderInfo.setActOrderStartTime(actForm.getActOrderStartTime());
        actOrderInfo.setActOrderEndTime(actForm.getActOrderEndTime());
        actOrderInfo.setActOrderCreatTime(new Date());
        actOrderInfo.setDoctorId(doctorId);
        actOrderInfo.setPatientAge(actForm.getPatientAge());
        actOrderInfo.setPatientSex(actForm.getPatientSex());
        actOrderInfo.setPatientIdCard(actForm.getPatientIdCard());
        actOrderInfo.setActOrderId(actOrderId);
        actOrderInfo.setPatientName(actForm.getPatientName());
        actOrderInfoMapper.insert(actOrderInfo);
    }
}
